Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix header forwarding #28

Merged
merged 10 commits into from
Jan 2, 2025
Merged

Fix header forwarding #28

merged 10 commits into from
Jan 2, 2025

Conversation

BoDonkey
Copy link
Contributor

Summary

Summarize the changes briefly, including which issue/ticket this resolves. If it closes an existing Github issue, include "Closes #[issue number]"
This PR refactors the handling of headers to allow for the excluding of the host header in single-site, multi-server instances. It also renames the header configuration arrays while maintaining backward compatibility.

What are the specific steps to test this change?

For example:

  1. Run the website and log in as an admin
  2. Open a piece manager modal and select several pieces
  3. Click the "Archive" button on the top left of the manager and confirm that it should proceed
  4. Check that all pieces have been archived properly

This has been tested in a Heroku two-server deployment. It has been tested in local dev deployment. It has not been tested in either Apostrophe hosting. It will be tested in multisite local deployment

What kind of change does this PR introduce?

(Check at least one)

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Build-related changes
  • Other

Make sure the PR fulfills these requirements:

  • It includes a) the existing issue ID being resolved, b) a convincing reason for adding this feature, or c) a clear description of the bug it resolves
  • The changelog is updated
  • Related documentation has been updated
  • Related tests have been updated

If adding a new feature without an already open issue, it's best to open a feature request issue first and wait for approval before working on it.

Other information:

Copy link
Member

@boutell boutell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small stuff thanks for seeing this through

],
excludeRequestHeaders: [
// For single-site setups or hosting on multiple servers, block the host header
'host'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right so it is critical to include this setting in the project level PRs.

README.md Outdated
### `forwardHeaders`
### `includeResponseHeaders`

An array of HTTP headers that you want to include from Apostrophe to the final response sent to the browser - useful if you want to use an Apostrophe module like `@apostrophecms/security-headers` and want to keep those headers as configured in Apostrophe.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also good for respecting apostrophe's caching headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

README.md Outdated
At the present time, Astro is not compatible with the `nonce` property of `content-security-policy` `script-src` value. So this is automatically removed with that integration. The rest of the CSP header remains unchanged.

### `excludeRequestHeaders`

An array of HTTP headers that you want to prevent from being forwarded from the browser to Apostrophe. This is particularly useful in single-site setups where you want to block the `host` header to allow Astro and Apostrophe to run on different domains or ports.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just say different hostnames. Different ports aren't really a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

const requestHeaders = {};
for (const [name, value] of req.headers) {
const headerLower = name.toLowerCase();
if (!config.excludeRequestHeaders?.map(h => h.toLowerCase()).includes(headerLower)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Shouldn't this be an equality comparison rather than "includes" which would match a header that happens to be a substring of some unrelated header?
  • Because it is on each request, it is worth making it more efficient by converting all of the excluded request headers to lowercase at startup rather than doing it over and over like this.
  • So you should be able to do a simple "includes" test on config.excludeRequestHeaders after you've normalized it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right! Good catch with the includes. Code refactored.

@BoDonkey BoDonkey requested a review from boutell January 2, 2025 14:39
@@ -7,10 +7,12 @@ export default async function aposResponse(req) {
const aposUrl = new URL(url.pathname, aposHost);
aposUrl.search = url.search;


const excludedHeadersLower = new Set(config.excludeRequestHeaders?.map(h => h.toLowerCase()) || []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move this to top level code in this file so it doesn't have to happen on every request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@BoDonkey BoDonkey requested a review from boutell January 2, 2025 20:34
@BoDonkey BoDonkey merged commit ae73035 into main Jan 2, 2025
BoDonkey added a commit that referenced this pull request Jan 2, 2025
@BoDonkey BoDonkey deleted the fix-header-forwarding branch January 2, 2025 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants